# -*- coding: utf-8 -*-
"""
@Time ： 2020/1/2 下午5:17
@Auth ： ydk
@File ：pipe_initial.py
@IDE ：PyCharm
"""
import subprocess as sp
import time
import cv2
import os

from config.config_setting import STREAM_SIZE, STREAM_FPS


class PipeInitial:
    def __init__(self, video_path=''):
        rtmpUrl = video_path
        devi = video_path.split('/')[-1]
        ffmpeg_log = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../log', 'ffmpeg'+str(devi) + '.log')
        stdout = open(ffmpeg_log, 'wb')

        self.command = ['ffmpeg',
                        '-y',
                        '-f', 'rawvideo',
                        '-vcodec', 'rawvideo',
                        '-pix_fmt', 'bgr24',
                        '-s', "{}x{}".format(STREAM_SIZE[0], STREAM_SIZE[1]),
                        '-r', str(STREAM_FPS),
                        '-i', '-',
                        '-c:v', 'libx264',
                        '-pix_fmt', 'yuv420p',
                        '-preset', 'ultrafast',
                        # '-c', 'copy',
                        '-f', 'flv',
                        '-tune', 'zerolatency',
                        rtmpUrl]
        self.p = sp.Popen(self.command, stdin=sp.PIPE, stdout=stdout.fileno(), stderr=stdout.fileno(), shell=False)
        print('pipe_initial  当前rtmp地址：', rtmpUrl)

    def get_pipe(self):
        p = sp.Popen(self.command, stdin=sp.PIPE, shell=False)
        return p

    def kill_pipe(self, p):
        p.kill()


